package com.example.lhh.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * @author luoHongHu
 * @Description redis工具类
 * @date 2025年05月16日 20:35
 */
@Component
public class RedisUtils {

    private static final long timeOutTime = 60000;//一分钟

    @Autowired
    private static RedisTemplate<String, Object> redisTemplate;

    public RedisUtils(RedisTemplate<String, Object> redisTemplate){
        this.redisTemplate = redisTemplate;

    }

    /**
     * 判断是否缓存过键
     * @param key
     * @return
     */
    public static boolean isHasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 通过key获取值
     * @param key
     * @return
     */
    public static Object getValueBykey(String key) {
        Object value = redisTemplate.opsForValue().get(key);
        return value == null ? "" : value;
    }

    /**
     * 删除制定key
     * @param key
     */
    public static void deleteKey(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 存入缓存
     * @param key
     * @param value
     */
    public static void setValue(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 存入缓存
     * @param key
     * @param value
     * @param timeOut 失效时间
     */
    public static void setValue(String key, String value, long timeOut) {
        if(timeOut > 0){
            redisTemplate.opsForValue().set(key, value, timeOut, TimeUnit.SECONDS);
        }else {
            redisTemplate.opsForValue().set(key, value,timeOutTime,TimeUnit.SECONDS);
        }
    }

    /**
     * 通过key获取值
     * @param key
     * @return
     */
    public static Object getValue(String key) {
        if(StringUtils.isEmpty(key)){
            return null;
        }
        try {
            return redisTemplate.opsForValue().get(key);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 写入数据
     * @param key
     * @param value
     * @return
     */
    public static boolean setObject(final String key, String value) {
        if (StringUtils.isEmpty(key)) {
            return false;
        }
        try {
            setObject(key, value , timeOutTime);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 写入数据
     * @param key
     * @param value
     * @param time 时间
     * @return
     */
    public static boolean setObject(String key, Object value, long time) {
        if (StringUtils.isEmpty(key)) {
            return false;
        }
        if (time > 0) {
            redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
        } else {
            redisTemplate.opsForValue().set(key, value, timeOutTime, TimeUnit.SECONDS);
        }
        return true;
    }

}
